package ar.edu.itba.it.pdc.nio;

import java.io.IOException;
import java.util.List;

import ar.edu.itba.it.pdc.configuration.ConfiguringProtocol;
import ar.edu.itba.it.pdc.configuration.ProxyConfigurationLoader;
import ar.edu.itba.it.pdc.monitoring.MonitoringProtocol;



public class TCPProxyServer {

	private static final int BUFSIZE = ProxyConfigurationLoader.getInstance().getValueAsInteger("buffer_size"); // Buffer size (bytes)
	
	public static void main(String[] args) throws IOException {
		
		runConfiguration();
		runMonitor();
		
		List<Integer> ports = ProxyConfigurationLoader.getInstance().getValuesAsInteger("proxy_ports");
		TCPProtocol protocol = new ProxySelectorProtocol();
		
		ProtocolRunner runner = new ProtocolRunner();
		
		runner.run(ports, protocol);
		
	}

	private static void runMonitor() {
		List<Integer> ports = ProxyConfigurationLoader.getInstance().getValuesAsInteger("monitoring_ports");
		
		TCPProtocol protocol = new MonitoringProtocol(BUFSIZE);
		
		ProtocolThread monitorThread = new ProtocolThread(ports, protocol);
		
		monitorThread.start();
	
	}

	private static void runConfiguration() {
		List<Integer> ports = ProxyConfigurationLoader.getInstance().getValuesAsInteger("config_ports");
		
		TCPProtocol protocol = new ConfiguringProtocol(BUFSIZE);
		
		ProtocolThread monitorThread = new ProtocolThread(ports, protocol);
		
		monitorThread.start();		
	}
}
